﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Input;
using BuildMonitor.Engine;

namespace BuildMonitor
{
	public partial class MainWindow : Window
	{

		public ConsoleText consoleText = new ConsoleText();
		public MainWindow()
		{
			InitializeComponent();
			ObjectDataProvider consoleData = FindResource("consoleData") as ObjectDataProvider;
			consoleData.ObjectInstance = consoleText;
		}

		private WindowState storedWindowState = WindowState.Normal;
		private void Window_StateChanged(object sender, EventArgs e)
		{
			if (WindowState == WindowState.Minimized)
			{
				ShowInTaskbar = false;
				if (notifyIcon != null)
					notifyIcon.ShowBalloonTip(2000);
			}
			else
			{
				storedWindowState = WindowState;
				ShowInTaskbar = true;
			}
		}

		private void notifyIcon_MouseClick(object sender, MouseButtonEventArgs e)
		{
			if (e.ChangedButton == MouseButton.Left)
			{
				WindowState = storedWindowState;
			}
		}


		private BuildWatcher watcher;
		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			consoleText.WriteLine("About to start the publisher");

			IBuildPublisher helper = PublisherMethod.GetPublisher(consoleText);
			watcher = new BuildWatcher(helper, consoleText);
			watcher.Start();

			consoleText.WriteLine("publisher started");
		}

		private void Window_Closing(object sender, CancelEventArgs e)
		{
			watcher.Stop();
		}
	}
}
